package com.thomasmoerman.dci3.frw

import org.springframework.transaction._
import org.springframework.transaction.support._ 
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Propagation._
import org.springframework.transaction.annotation.Isolation
import org.springframework.transaction.annotation.Isolation._
import org.springframework.beans.factory.annotation._
import scala.reflect._

trait Transactional {
  
  def inTransaction[A](propagation: Propagation = REQUIRED, isolation: Isolation = DEFAULT)(block: => A): A
  
}

trait TransactionalWithSpring extends Transactional {
  this: TransactionManagerComponent =>
  
  def inTransaction[A](propagation: Propagation = REQUIRED, isolation: Isolation = DEFAULT)(block: => A): A = {
	val template = new TransactionTemplate(transactionManager)
	
	template.setPropagationBehavior(propagation.value)
	template.setIsolationLevel(isolation.value)
	
	template.execute{
	  new TransactionCallback[A] {
	    def doInTransaction(status: TransactionStatus) = block
      }	
	}
  }
  
}